package cn.iocoder.yudao.module.bpm.service.tohandle.sourceconfig.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig.vo.BpmHandleSourceConfigCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig.vo.BpmHandleSourceConfigExportReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig.vo.BpmHandleSourceConfigPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig.vo.BpmHandleSourceConfigUpdateReqVO;
import cn.iocoder.yudao.module.bpm.convert.tohandle.sourceconfig.BpmHandleSourceConfigConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tohandle.BpmHandleSourceConfigDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.tohandle.BpmHandleSourceConfigMapper;
import cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.bpm.service.tohandle.sourceconfig.BpmHandleSourceConfigService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 审批流待办中心——来源系统配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BpmHandleSourceConfigServiceImpl implements BpmHandleSourceConfigService {

    @Resource
    private BpmHandleSourceConfigMapper handleSourceConfigMapper;

    @Override
    public Long createHandleSourceConfig(BpmHandleSourceConfigCreateReqVO createReqVO) {
        // 插入
        BpmHandleSourceConfigDO handleSourceConfig = BpmHandleSourceConfigConvert.INSTANCE.convert(createReqVO);
        handleSourceConfigMapper.insert(handleSourceConfig);
        // 返回
        return handleSourceConfig.getId();
    }

    @Override
    public void updateHandleSourceConfig(BpmHandleSourceConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateHandleSourceConfigExists(updateReqVO.getId());
        // 更新
        BpmHandleSourceConfigDO updateObj = BpmHandleSourceConfigConvert.INSTANCE.convert(updateReqVO);
        handleSourceConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteHandleSourceConfig(Long id) {
        // 校验存在
        validateHandleSourceConfigExists(id);
        // 删除
        handleSourceConfigMapper.deleteById(id);
    }

    private void validateHandleSourceConfigExists(Long id) {
        if (handleSourceConfigMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.HANDLE_SOURCE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public BpmHandleSourceConfigDO getHandleSourceConfig(Long id) {
        return handleSourceConfigMapper.selectById(id);
    }

    @Override
    public List<BpmHandleSourceConfigDO> getHandleSourceConfigList(Collection<Long> ids) {
        return handleSourceConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BpmHandleSourceConfigDO> getHandleSourceConfigPage(BpmHandleSourceConfigPageReqVO pageReqVO) {
        return handleSourceConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmHandleSourceConfigDO> getHandleSourceConfigList(BpmHandleSourceConfigExportReqVO exportReqVO) {
        return handleSourceConfigMapper.selectList(exportReqVO);
    }

}
